<?php
namespace Home\Controller;
use Think\Controller;
/**
 *活动产品控制器
 *@author [blakefan] <1621743934@qq.com>
 */
class ActivProductController extends Controller
{
	/*
	 *活动产品列表
	 *@id 活动ID
	 *return json
	 */
	
	public function index()
	{
		$id=I('get.id',1,'strip_tags');
		$page=I('get.page',1,'strip_tags');

		$result='';
		if (empty($id)) {
			$result['status']=2;
			$result['msg']='参数错误';
			echo json_encode($result,JSON_UNESCAPED_UNICODE);
			return '';
		}
		$activ_item= M('activ_item a');
		$count=M('activ_item')->where('cid=%d',$id)->count();

		$pagearr= getPageArr($page,24,$count);

		$products=$activ_item->join('inner join '.C('DB_PREFIX').'item b  on  a.itemid=b.itemid ')
						 ->where('a.cid=%d',$id)
						 ->order('b.itemid desc')
						 ->limit($pagearr['start'],$pagearr['pagesize'])
						 ->getField('a.id,b.itemimg,b.price,a.activ_price,a.res_inventory');
		$result['status']=!empty($products)?1:3;
		$result['products']=!empty($products)?array_values($products):array();
		$result['totalpage']=!empty($pagearr['pagetotal'])?$pagearr['pagetotal']:1;
		$result['msg']=!empty($products)?'success':'暂无数据';

		echo json_encode($result,JSON_UNESCAPED_UNICODE);
	}

	/*
	 ×活动产品详情
	 ×id 活动产品表ID 
	 *return json
	 */
	public function productDetail(){
		$result='';
		$id =I('get.id',0,'strip_tags'); // 活动产品id
		$id=intval($id);
		
		if(empty($id))
		{
			$result['status']=0;
			$result['msg']='参数错误';
			echo json_encode($result,JSON_UNESCAPED_UNICODE);
			return '';
		}
		
		$res=M()->query('select itemid,productid,activ_price,res_inventory,limit_num from '.C('DB_PREFIX').'activ_item where id='.$id);
		
		if(empty($res))
		{
			$result['status']=2;
			$result['iteminfo']=array();
			$result['msg']='暂无数据';
			echo json_encode($result,JSON_UNESCAPED_UNICODE);
			return '';
		}

		$itemid=$res[0]['itemid'];
		$productid=$res[0]['productid'];

		$item=M('item a');
		//获取活动商品基本信息
		$iteminfo=$item->join('inner join '.C('DB_PREFIX').'item_desc b on a.itemid=b.itemid')
             ->where('a.itemid=%d',$itemid)
		     ->getField('a.itemid,a.itemname,a.bn,a.price,a.itemimg');
		//获取活动商品活动价格，剩余库存
		$res[0]['color']=M('product')->where('productid=%d',$productid)->getField('color');
		
		
		$iteminfo=array_values($iteminfo);

		if(empty($iteminfo))
		{
			$result['status']=2;
			$result['iteminfo']=array();
			$result['msg']='暂无数据';
			echo json_encode($result,JSON_UNESCAPED_UNICODE);
			return '';
		}
		
		$result['status']=1;
		$result['msg']='成功';
		$result['iteminfo']=array_merge($res[0],$iteminfo[0]);
		echo json_encode($result,JSON_UNESCAPED_UNICODE);
		return '';

	}

	//获取商品webview中的详情信息
	public function getItemHtml()
	{
		$result='';
		$id =I('get.id',0,'strip_tags'); // 活动产品id
		$id=intval($id);
		
		if(empty($id))
		{
			$temp='暂无数据';
			echo $temp;
			return '';
		}
		
		$res=M()->query('select itemid,productid from '.C('DB_PREFIX').'activ_item where id='.$id);
		
		if(empty($res))
		{
			$temp='暂无数据';
			echo $temp;
			return '';
		}

		$itemid=$res[0]['itemid'];
		$productid=$res[0]['productid'];
		$item=M('item_desc');
		$temp=$item->where('itemid=%d',$itemid)->getField('itemdesc');
		if (!empty($temp))
		{
			echo "<style type='text/css'> p{margin-top:0px;margin-bottom:0px;}</style>".$temp;
			return '';
		
		}
		$temp='暂无数据';
		echo $temp;
		
	}

}